<?php

namespace App\Jobs;

use App\Models\DahuaTokenModel;
use App\Models\DahuaVideoModel;
use App\Models\PetsFoodLogsModel;
use App\Models\PetsModel;
use App\Models\UserModel;
use App\Services\CurlBasiceService;
use App\Tools\AliyunUploadTool;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class Oss implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**

     * 任务最大尝试次数。

     *

     * @var int

     */

    public $tries = 3;

    /**

     * 任务运行的超时时间。

     *

     * @var int

     */

    public $timeout = 3000;


    protected $data;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        writeLoToFile('oss', 'Queue：', $this->data);
        $value = json_decode($this->data, true);

        $result = DahuaTokenModel::getSingle();

        unset($data);
        $header = $result['token'];
        $data['taskId'] = $value['taskId'];
        $data['pageNum'] = 1;
        $data['pageSize'] = 10;
        $result1 = CurlBasiceService::httpPost('https://www.cloud-dahua.com/gateway/cloudstorage/api/transfer/taskFilesById', json_encode($data), $header);
        writeLoToFile('oss', '创建2：', json_encode($result1));

        try {
            if (isset($result1['code']) && $result1['code'] == 0 && !empty($result1['data']['fileList'])) {
                $urlArr = [];
                $ossUrlArr = [];

                foreach ($result1['data']['fileList'] as $v) {
                    if (empty($v['url'])) {
                        //跳到下一个循环
                        continue;

                    }

                    //上传到oss
                    //本地下载路径
                    $qrNameOld = time() . rand(1000, 9999) . $value['taskId'];
                    $qrName = $qrNameOld . '.mp4';
                    $qrOldName = $qrNameOld . 'old.mp4';
                    $qrPath = public_path('video/' . $qrName);
                    $qrOldPath = public_path('video/' . $qrOldName);
                    $content = file_get_contents($v['url']);
                    file_put_contents($qrPath, $content);

                    writeLoToFile('oss', '下载：', json_encode($qrPath));

                    //转码
                    exec('/usr/local/ffmpeg/bin/ffmpeg -ss 00:00:00 -to 00:05:00 -i ' . $qrPath . ' -threads 5 -preset ultrafast ' . $qrOldPath);

                    //删除原视频
                    unlink($qrPath);
                    writeLoToFile('oss', '转码：', '成功');

                    //oss路径
                    $object = 'api/dahua/' . date('Ymd', time()) . '/' . $qrName;
                    (new AliyunUploadTool())->uploadFile('farm-sheep', $object, $qrOldPath);
                    $pix = config('wechat.oss.url');
                    $petsQrcode = $pix . $object;

                    $urlArr[] = $v['url'];
                    $ossUrlArr[] = $petsQrcode;

                    writeLoToFile('oss', '上传完成：', json_encode($object));

                }
                writeLoToFile('oss', '修改数据库：', json_encode([
                    'status' => 4,
                    'url' => $urlArr,
                    'oss_url' => $ossUrlArr

                ]));


                //修改数据
                DahuaVideoModel::updateSingle([
                    'id' => $value['id']
                ],
                    [
                        'status' => 4,
                        'url' => $urlArr,
                        'oss_url' => $ossUrlArr

                    ]);
                echo '创建完成';

            } else {
                //修改数据
                DahuaVideoModel::updateSingle([
                    'id' => $value['id']
                ],
                    [
                        'status' => 5,
                    ]);
                echo '创建失败';

            }
        }catch (\Exception $e){
            writeLoToFile('oss', '报错：', json_encode($e));
        }


    }
}
